//
//  DemandTheHallRightTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/12/5.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "DemandTheHallRightTabVC.h"
#import "DemandCell.h"
#import "DemandOnlineOfflineCell.h"
#import "HomeModel.h"
@interface DemandTheHallRightTabVC ()<DemandCellDelegate>
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,strong) NSMutableArray *demandArray;
@property(nonatomic,strong) NSMutableArray *believeArray;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *stringUrl;
@property(nonatomic,assign) NSInteger pageNum;
@property(nonatomic,assign) NSInteger pageSize;

@end
static NSString *identifierNeed = @"DemandCell";
static NSString *identifierNeedSoldOut = @"DemandOnlineOfflineCell";
@implementation DemandTheHallRightTabVC
-(NSMutableArray *)demandArray
{
    if (!_demandArray) {
        _demandArray = [NSMutableArray array];
        
    }
    return _demandArray;
}
-(NSMutableArray *)believeArray
{
    if (!_believeArray) {
        _believeArray = [NSMutableArray array];
        
    }
    return _believeArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [AFHTTPSessionManager manager];
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    [self.tableView registerNib:[UINib nibWithNibName:@"DemandCell" bundle:nil] forCellReuseIdentifier:identifierNeed];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_header = header;
    [header beginRefreshing];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
    self.tableView.mj_footer = footer;
                [footer setTitle:[NSBundle bundleLocalizedStringForKey:@"footerTitle"]
 forState:MJRefreshStateNoMoreData];
                footer.stateLabel.font = [UIFont systemFontOfSize:12];
                footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    self.ass = @"3";
    self.assub = @"1";
    
    
}
-(void)refresh
{
    self.demandArray = nil;
    self.believeArray = nil;
    self.pageNum = 1;
    self.pageSize = 10;
    [self reloadInternetData];
    
}
-(void)reloadMore
{
    self.pageNum ++;
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    [self reloadInternetData];
    
}

#pragma mark-需求内容
-(void)reloadInternetData
{
    
    NSString *pageNums = [NSString stringWithFormat:@"%ld",(long)self.pageNum];
    NSString *pageSizes = [NSString stringWithFormat:@"%ld",(long)self.pageSize];
    self.dict = @{
                  @"pageNum":pageNums,
                  @"pageSize":pageSizes,
                  @"ass":self.ass,
                  @"assub":self.assub,
                  @"uid":@"1"
                  };
    XLog(@"self.dict:%@",self.dict);
    
    self.stringUrl = [NSString stringWithFormat:@"%@/demand/selectTranslateDemand",kUrl];
    [self.manager POST:self.stringUrl parameters:self.dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        XLog(@"responseObject:%@",responseObject);
        NSArray *temp = [responseObject objectForKey:@"date"];
        XLog(@"temp:%@",temp);
        if (![temp isKindOfClass:[NSNull class]]) {
            
            for (NSDictionary *dict in temp) {
                HomeModel *model =[HomeModel mj_objectWithKeyValues:dict];
                [self.demandArray addObject:model];
            }
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            XLog(@"self.demandArray:%ld",self.demandArray.count);
        }else {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            XLog(@"22");
        }
        [self.tableView reloadData];
        [WSProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XLog(@"error:%@",error);
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.demandArray?self.demandArray.count:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    DemandCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierNeed forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    HomeModel *model = self.demandArray[indexPath.row];
    cell.delegate = self;
    cell.indexPath = indexPath;
    cell.homeModel = model;
    //    HomeModel *believeModel = self.believeArray[indexPath.row];
    //    cell.believeModel = believeModel;
    return cell;
    
}
#pragma mark-代理:显示联系人
-(void)demandCell:(DemandCell *)demandCell withClickBtn:(UIButton *)clickBtn withIndexPath:(NSIndexPath *)indexPath withHomeModel:(HomeModel *)homeModel
{
    HomeModel *model = self.demandArray[indexPath.row];
    model.show = clickBtn.selected;
    XLog(@"model.show:%d",model.show);
    //    [self.tableView reloadData];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
}
#pragma mark-代理:收藏
- (void)demandCell:(DemandCell *)demandCell withCollectBtn:(UIButton *)clickBtn withIndexPath:(NSIndexPath *)indexPath withHomeModel:(HomeModel *)homeModel
{
    HomeModel *model = self.demandArray[indexPath.row];
    model.collectStatus = clickBtn.selected;
    //    [self.tableView reloadData];
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (!self.releaseBool) {
        return 144;
    }else {
        return 150;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.returnValueBlock) {
        self.homeModel = self.demandArray[indexPath.row];
        XLog(@"self.homeModel:%@",self.homeModel.represent);
        self.returnValueBlock(indexPath.row);
    };
}
@end
